package com.matrix.media.entity.Audio;

import com.alibaba.fastjson.annotation.JSONField;
import com.matrix.media.entity.Effect.AIASREffect;
import com.matrix.media.entity.Effect.Effect;
import com.matrix.media.entity.Effect.VolumeEffect;
import com.matrix.media.entity.FontFace;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class AudioTrack {

    //用于指定当前轨道是否为主轨道。默认为 False。详细介绍及示例请参见设置主轨道。
    @JSONField(name = "MainTrack")
    private Boolean MainTrack;

    //音频轨素材片段列表。
    @JSONField(name = "AudioTrackClips")
    private List<AudioTrackClip> AudioTrackClips;


    /**
     * 添加背景音乐
     * @param urlString
     */
    public void addBgMusic(String urlString) {
        DefaultAudioTrackClip audioTrackClip = new DefaultAudioTrackClip();
        audioTrackClip.setMediaURL(urlString);
        addAudioTrackClip(audioTrackClip);
    }

    /**
     * 添加人声
     * @param text
     * @param voice
     * @param fontName
     * @param fontSize
     * @param fontColor
     */
    //    String audioClip = "{\"AudioTrackClips\":[{\"Type\":\"AI_TTS\",\"Content\":\"" + text + "\",\"Voice\":\"sicheng\",\"Effects\":[{\"Type\":\"AI_ASR\",\"Font\":\"" + fontName + "\",\"Alignment\":\"BottomCenter\",\"Y\":0.1,\"FontSize\":" + fontSize + ",\"FontColor\":\"" + fontColor + "\",\"FontFace\":{\"Bold\":true,\"Italic\":false,\"Underline\":false}},{\"Type\":\"Volume\",\"Gain\":10}]}]}";
    public void addVoice(String text, String voice, String fontName, int fontSize, String fontColor) {
        AITTSAudioTrackClip audioTrackClip = new AITTSAudioTrackClip();
        audioTrackClip.setType("AI_TTS");
        audioTrackClip.setContent(text);
        audioTrackClip.setVoice(voice);

        List<Effect> effects = new ArrayList<>();

        //人声
        AIASREffect effect = new AIASREffect();
        effect.setType("AI_ASR");
        effect.setFont(fontName);
        effect.setAlignment("BottomCenter");
        effect.setFontSize(fontSize);
        effect.setFontColor(fontColor);
        effect.setY((float) 0.1);

        FontFace fontFace = new FontFace();
        fontFace.setBold(true);
        fontFace.setUnderline(false);
        effect.setFontFace(fontFace);
        effects.add(effect);

        //音量
        VolumeEffect volumeEffect = new VolumeEffect();
        volumeEffect.setGain((float) 10);
        effects.add(volumeEffect);

        audioTrackClip.setEffects(effects);

        addAudioTrackClip(audioTrackClip);
    }


    private void addAudioTrackClip(AudioTrackClip audioTrackClip) {
        if (AudioTrackClips == null) {
            synchronized (this) {
                if (AudioTrackClips == null) {
                    AudioTrackClips = new ArrayList<>();
                }
            }
        }
        // 在这里添加您的添加逻辑...
        if (audioTrackClip == null) {
            // 处理空值情况，例如抛出异常或返回错误
            throw new IllegalArgumentException("audioTrackClip cannot be null");
        }

        // 检查类型是否正确，例如：
        if (!(audioTrackClip instanceof AudioTrackClip)) {
            // 处理类型不匹配情况，例如抛出异常或返回错误
            throw new IllegalArgumentException("audioTrackClip must be an instance of AudioTrackClip");
        }

        // 在这里添加您的添加逻辑...
        AudioTrackClips.add(audioTrackClip);
    }

}
